//
//  TabBarViewController.m
//  WeiBo
//
//  Created by lanou3g on 15/6/19.
//  Copyright (c) 2015年 杨建. All rights reserved.
//

#import "TabBarViewController.h"
#import "NavigationController.h" // 导航栏
#import "HomeViewController.h" // 首页
#import "DiscoveryViewController.h" // 发现
#import "EntertainmentViewController.h" // 娱乐

@interface TabBarViewController ()
{
    HomeViewController *home;
    EntertainmentViewController *entertainment;
    DiscoveryViewController *discover;
    
}


@property (nonatomic, strong)HomeViewController *home;



@end

@implementation TabBarViewController

// 很多重读代码 ----> 将重复代码抽取到一个方法中
// 1.相同的代码放到一个方法中
// 2.不同的东西变成参数
// 3.在使用到这段代码的这个地方调用方法 传递参数

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.初始化自控器
    home = [[HomeViewController alloc] init];
    [self addChildVc:home Title:@"资讯" image:@"MainTabBar_news_normal" selectedImage:@"MainTabBar_news_active"];
    
    entertainment = [[EntertainmentViewController alloc] init];
    [self addChildVc: entertainment Title:@"娱乐" image:@"MainTabBar_user_normal" selectedImage:@"MainTabBar_user_active"];
    
    discover = [[DiscoveryViewController alloc] init];
    [self addChildVc:discover Title:@"发现" image:@"MainTabBar_found_normal" selectedImage:@"MainTabBar_found_active"];
    
}

/**
 *  添加一个自控制器
 *
 *  @param childVc       自控自弃
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */

- (void)addChildVc:(UIViewController *)childVc Title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置自控器的图片和文字
    childVc.title = title; // 同时设置tabar 和navigationBar文字
    
    // 设置控制器图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    // 声明:这张图片按照原始的样子显示出来,不要自动渲染成其他颜色（比如蓝色）
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 抽屉样式
    childVc.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self Action:@selector(leftDrawerButtonPress:) image:@"myhomeh" highImage:@"myhome"];

    // 先给外面传进来的小控制器,包装 一个导航控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childVc];

    
    // 添加为子控制器
    [self addChildViewController:nav];
}

//    抽屉left按钮触发的方法
-(void)leftDrawerButtonPress:(id)sender
{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];

}

#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}



@end
